
moba.projectiles = {}

function moba.projectiles_destroy(i)
	freeimage(moba.projectiles[i].image)
	moba.projectiles[i] = nil
end

function moba.projectiles_spawn(px,py,srct,src,srcn,tt,t,damage,speed,img)
	local projectiles = moba.projectiles
	for i=1,#projectiles+1 do
		if not projectiles[i] then
			projectiles[i] = {
				x = px,
				y = py,
				target = t,
				target_t = tt,
				src_id = src,
				src_t = srct,
				src_n = srcn,
				speed = speed,
				damage = damage,
				image = image(img,0,1,3),
			}
			return i
		end
	end
end

addhook("always","moba.projectiles_always")
function moba.projectiles_always()
	for k,proj in pairs(moba.projectiles) do
		local dx = moba.id_field(proj.target_t, proj.target, "x")-proj.x
		local dy = moba.id_field(proj.target_t, proj.target, "y")-proj.y
		local dist = (dx*dx+dy*dy)^0.5
		local srct = proj.src_t
		if dist < 24 then
			if proj.target_t == "players" then
				if srct == "players" then
				else
					moba.physdmgplayer(proj.target,proj.src_n,proj.damage)
				end
			end
			moba.projectiles_destroy(k)
		else
			proj.x = proj.x + (dx/dist) * proj.speed
			proj.y = proj.y + (dy/dist) * proj.speed
			imagepos(proj.image, proj.x, proj.y, math.deg(math.atan2(dy,dx))-90)
		end
	end
end